<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>

<body>
    <script>
        // 数组深克隆
        var arr1 = [33, 44, 11, 22, [77, 88]];
        //函数，这个函数会被递归
        function deepCloneArr(arr) {
            var result = [];
            // 类型判断， 如果是基本数据类型直接push如果是引用数据类型则重复浅克隆操作
            for (var i = 0; i < arr.length; i++) {
                if (Array.isArray(arr[i])) {
                    //递归
                    result.push(deepClone(arr[i]));
                } else {
                    result.push(arr[i]);
                }
            }
            return result;
        }

        var obj1 = {
            a: 1,
            b: 2,
            c: [
                22,
                33, {
                    m: 23,
                    n: 98,
                    p: [77, 88],
                },
            ],
        };
        // 对象深克隆
        function deepCloneObj(o) {
            // 判断o是对象还是数组
            if (Array.isArray(o)) {
                // 数组
                var result = [];
                for (var i = 0; i < o.length; i++) {
                    result.push(deepCloneObj(o[i]));
                }
            } else if (typeof o == "object") {
                // 对象
                var result = {};
                for (var k in o) {
                    result[k] = deepCloneObj(o[k]);
                }
            } else {
                // 基本类型数据
                var result = o;
            }
            return result;
        }
        var obj2 = deepCloneObj(obj1);
        console.log(obj2);
    </script>
</body>

</html>